<%@ page language="java" %>
<%
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("max-age", 0);
    response.setDateHeader("Expires", 0);
%>

<%@ page import="com.scalar.chiptrack.utils.ConfigManager,
                 com.scalar.chiptrack.users.User,
                 com.scalar.chiptrack.commons.SessionKeys"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>

<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>


<html>

<HEAD>
	<TITLE>ScalarSoft - ChipTrac - Fab Wafer Data Entry </TITLE>
    <META http-equiv='Content-Type' content='text/html; charset=windows-1252'>
    <LINK href='<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css' type='text/css' rel='stylesheet'>
	<script src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>
    <script src="<%=chipTracContext%>/jsp/includes/scripts/functions.js"></script>
    <script language='javascript' >

	function resetValues()
    {
        document.fabWaferDataEntryform.reset();
    }

    function save()
	{
        errors  = "";
        var pa  = document.getElementById("message");
        pa.innerHTML = '';
        success = false;

        form		    = document.fabWaferDataEntryform;
		openerform	    = window.opener.document.fabDataEntry;
        var openerFormWaferIdsCtrl= openerform.waferId;
        var waferIdCtrl =  form.waferId;
        if ( waferIdCtrl.length != null )
        {
            var waferIds = "";
            for ( i = 0; i < waferIdCtrl.length; i++ )
            {
                //START: validating wafer id
                if ( waferIdCtrl[i].value.trim() == '' )
                {
                    pa.innerHTML = "<li>Please enter value for Wafer Id</li>";
                    waferIdCtrl[i].select();
                    return;
                }
                if ( !isInteger( waferIdCtrl[i].value ) )
                {
                    pa.innerHTML = "<li>Please enter valid Wafer Id, Wafer Id must be number</li>";
                    waferIdCtrl[i].select();
                    return;
                }

                if ( parseInt ( waferIdCtrl[i].value, 10 ) == 0 )
                {
                    pa.innerHTML = "<li>Zero value is not permitted for Wafer Id, Please enter greater than Zero</li>";
                    waferIdCtrl[i].select();
                    return;
                }
                //END
            }
            //START:TO FIND DUPLICATE WAFER IDS
            for ( i = 0; i < waferIdCtrl.length; i++ )
            {
                var tempWaferId = waferIdCtrl[i].value;
                for ( j = (i+1); j < waferIdCtrl.length; j++ )
                {
                    if ( parseInt ( waferIdCtrl[i].value, 10 ) == parseInt ( waferIdCtrl[j].value, 10 ) )
                    {
                        pa.innerHTML = "<li>Wafer Id has duplicates</li>";
                        waferIdCtrl[j].select();
                        waferIdCtrl[j].focus();
                        return;
                    }
                }
            }
            //END
            //START: ASSIGNING WAFER IDS TO THE PARENT WINDOW
            waferIds = "";
            for ( i = 0; i < waferIdCtrl.length; i++ )
            {
                waferIds = waferIds + waferIdCtrl[i].value.trim() + ";" ;
            }
            //END
            openerFormWaferIdsCtrl.value = waferIds;
        }
        else
        {
            //START: validating wafer id
            if ( waferIdCtrl.value.trim() == '' )
            {
                pa.innerHTML = "<li>Please enter value for Wafer Id</li>";
                waferIdCtrl.select();
                return;
            }
            if ( !isInteger( waferIdCtrl.value  ) )
            {
                pa.innerHTML = "<li>Please enter valid Wafer Id, Wafer Id must be number</li>";
                waferIdCtrl.select();
                return;
            }
            if ( parseInt ( waferIdCtrl.value , 10 ) == 0 )
            {
                pa.innerHTML = "<li>Zero value is not permitted for Wafer Id, Please enter greater than Zero</li>";
                waferIdCtrl.select();
                return;
            }
            //END
            openerFormWaferIdsCtrl.value = waferIdCtrl.value + ";" ;
        }
       closeWindow();
    }
    function closeWindow()
    {
        window.close();
    }
    function onLoad()
    {
        form		    = document.fabWaferDataEntryform;
		openerform	    = window.opener.document.fabDataEntry;
        var openerFormWaferIdsCtrl= openerform.waferId;
        var waferIdCtrl =  form.waferId;
        var enteredWaferIdsArr = openerFormWaferIdsCtrl.value.split(";");
        //START: TO POPULATE ALREADY ENTERED VALUES
        if ( waferIdCtrl != null )
        {
            if ( waferIdCtrl.length )
            {
                var waferIdsCount       = waferIdCtrl.length;
                var enteredwaferIdsCount= enteredWaferIdsArr.length;
                var minCount    = ( waferIdsCount > enteredwaferIdsCount ) ? enteredwaferIdsCount : waferIdsCount;
                for ( i = 0; i < minCount; i++ )
                {
                    waferIdCtrl[i].value = enteredWaferIdsArr[i];
                }
            }
            else
            {
                waferIdCtrl.value = enteredWaferIdsArr[0]
            }
        }
        //END
    }
    </script>
</HEAD>
<%
    User user = ( User ) session.getAttribute ( SessionKeys.USER_KEY );
    if ( user != null )
    {
%>
<BODY bgcolor='ffffff' marginheight='0' marginwidth='0' topmargin='0' leftmargin='0' onLoad="onLoad()">

<table align='center' border='0' width='100%' >
    <TR>
        <td align='center' class="NArialL">Fab Wafer Data Entry</td>
    </TR>
    <TR>
        <TD align='center'>&nbsp;</TD>
    </TR>
    <form name="fabWaferDataEntryform" action="">
    <%
        int waferQty = 0;
        try
		{
            waferQty = Integer.parseInt( request.getParameter( "waferQty" ) );
        }
		catch( Exception ex )
		{
            waferQty = 0;
        }

        if( waferQty == 0 )
		{
    %>
        <TR>
            <TD align='center'><font class="NArialL" color="red">Wafer Qty Should be Greater zero / Not Entry </font></TD>
        </TR>
        <TR>
            <TD align='center'><font class="NArialL">&nbsp;</font></TD>
        </TR>
        <TR>
            <TD align='center'><a href="javascript:close()" title='Click here to close the window'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" ></a></TD>
        </TR>
    <%
        }
		else
		{
    %>
        <TR><TD align='center'>
			<TABLE align="center" border="0" width="80%">
				<TR>
				<TD colspan="2" width="100%" class="NArialL">&nbsp;<FONT id="message" size=1 color='FF0000'></FONT></TD>
				</TR>
                <TR> <TD align='center' bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF"> SL.No </font></font></TD>
                    <TD align='center' bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF"> Wafer Id *</font></font></TD>
                </TR>

            <%
                for( int count = 0; count < waferQty ; count++ )
                {
            %>
                  <tr>
                      <td align='center'><font class="NArialL"><%= count+1 %> </font></td>
                      <td align='center'><font class="NArialL"><input type="text" name="waferId" value="" size='10' maxlength='50' class="valuetxfield" onkeypress="allowInteger()" ></font></td>
                 </tr>
            <%
                }
            %>
            </TABLE>
            </TD>
        </TR>
        <TR><TD align='center'>&nbsp;</TD></TR>
        <TR>
            <TD align='center'>
                    <a href="javascript:save()" title='Click here to Save Fab Wafer Data Entry'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/save.gif" ></a>&nbsp;&nbsp;&nbsp;
                    <a href="javascript:resetValues()" title='Click here to reset all field values'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/reset.gif" ></a>&nbsp;&nbsp;&nbsp;
                    <a href="javascript:window.close()" title='Click here to close the window'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" ></a>
            </TD>
        </TR>
    </form>
    <%
      }
    %>
</table>
</body>
<%
    }
    else
    {
%>
    <BODY bgcolor='ffffff' marginheight='0' marginwidth='0' topmargin='0' leftmargin='0' >
    <TABLE cellpadding='0' cellspacing='0' bgcolor='FDF5E6' border="0" width="100%"  height="100%" align=center>
        <TR>
            <TD>
                <TABLE align="center" border="0" width="40%" bgcolor="#FDF5E6">
                    <TR><TD>&nbsp;</TD></TR>
                    <TR><TD align="center" ><font class="NArialL" ><font color="red"><b>Session has been time out , Please close this window and login again.<b></font></font></TD></TR>
                    <TR><TD>&nbsp;</TD></TR>
                    <TR><TD align="center" ><a href="javascript:window.close();"><font class='NArialL'>[Close this window]</font></a></TD></TR>
                </TABLE>
            </TD>
       </TR>
       <TR><TD>&nbsp;</TD></TR>
    </TABLE>
    </BODY>
<% } %>
</html>
